package com.handong.handserialprogram.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.handong.handserialprogram.module.serialport.SerialPortUtil;
import com.handong.handserialprogram.utils.EventBusUtil;
import com.handong.handserialprogram.utils.JLog;


public class LocateService extends Service {

    private static final String TAG = LocateService.class.getSimpleName();

    public static final double LOCATE_ERROR = 4.9E-324;
    private LocateBinder mLocateBinder;

    private AMapLocationClient mBDLocationClient;
    private MyLocationListener mLocationListener;
    private double mLatitude, mLongitude;

    private SerialPortUtil mSerialPortUtil;
    protected long mLastTime = 0L;

    public LocateService() {
    }

    @Override
    public void onCreate() {
        JLog.d(TAG, "onCreate");
        super.onCreate();
        mLocateBinder = new LocateBinder();
        //开始定位。
        try {
            mBDLocationClient = new AMapLocationClient(getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        AMapLocationClientOption option = new AMapLocationClientOption();
        mLocationListener = new MyLocationListener();
        mBDLocationClient.setLocationListener(mLocationListener);
        option.setGpsFirst(true);
        option.setInterval(2500);
//        option.setOpenGps(true); // 打开gps
//        option.setCoorType("bd09ll"); //设置坐标类型
//        option.setScanSpan(2500);
        option.setNeedAddress(true);//需要位置语义化结果

        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mBDLocationClient.setLocationOption(option);
        mBDLocationClient.startLocation();//开始定位。

        mSerialPortUtil = SerialPortUtil.getInstance(getApplicationContext());
    }

    public void calc(int a, int b) {
        JLog.d(TAG, "Result:" + (a + b));
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        JLog.d(TAG, "onTaskRemoved");
        //最近列表滑动关闭，释放资源。
        mSerialPortUtil.power_Off();
        super.onTaskRemoved(rootIntent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mLocateBinder;
    }

    public class LocateBinder extends Binder {

        public LocateService getService() {
            return LocateService.this;
        }
    }

    @Override
    public void onDestroy() {
        if (mBDLocationClient != null) {
            mBDLocationClient.stopLocation();
            mBDLocationClient.unRegisterLocationListener(mLocationListener);
            mBDLocationClient = null;
        }
        mLastTime = 0L;
        mSerialPortUtil = null;
        JLog.d(TAG, "onDestroy");
        super.onDestroy();
    }

    /**
     * 定位SDK监听
     */
    public class MyLocationListener implements AMapLocationListener {

        @Override
        public void onLocationChanged(AMapLocation location) {
            // map view 销毁后不在处理新接收的位置 4.9E-324, mLongitude:4.9E-324
            if (mSerialPortUtil == null) {
                return;
            }
            if (location.getErrorCode() != 0) {
                JLog.d(TAG, "定位失败" + location.getErrorCode()+"info:"+location.getErrorInfo());
                return;
            }
            long time = System.currentTimeMillis();
            if (mLastTime != 0L && time - mLastTime < 800){
                return;
            }
            mLastTime = time;
            mLatitude = location.getLatitude();
            mLongitude = location.getLongitude();
            //mLocationListener.onLocationChanged(location);// 显示系统小蓝点,不写这一句无法显示到当前位置

            mSerialPortUtil.setLocationLatLng(mLatitude, mLongitude);
            //JLog.d(TAG, "GPS success");
            EventBusUtil.post(location);
        }
    }
}
